//
//  ReportOrders.m
//  iHedron
//
//  Created by George Radev on 2011-08-09.
//  Copyright 2011 I Me Mine. All rights reserved.
//

#import "ReportOrders.h"
#import "HedronCore.h"
#import "OrderStockCell.h"
#import "OrderEdit.h"


@implementation ReportOrders

- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Поръчки";
}

- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
}

#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[HedronCore getReportOrders] count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [(NSArray*)[[HedronCore getReportOrders] objectAtIndex: section] count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	int section = indexPath.section;
	int row = indexPath.row;
	
	static NSString *OrderStockCellIdentifier = @"OrderStockCell";
	OrderStockCell *cell = (OrderStockCell*)[tableView dequeueReusableCellWithIdentifier:OrderStockCellIdentifier];
	if (cell == nil) {
		NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"OrderStockCell" owner:nil options:nil];
		
		for(id currentObject in topLevelObjects){
			if([currentObject isKindOfClass:[UITableViewCell class]]){
				cell = (OrderStockCell*) currentObject;
				break;
			}
		}
	}
	
	// Configure the cell...
	if(cell){
		Order* order = (Order*)[(NSArray*)[[HedronCore getReportOrders] objectAtIndex: section] objectAtIndex: row];
		cell.stockName.text = order.ocustomer;
		cell.stockPrice.text = [NSString stringWithFormat: @"%.2f", order.ototal];
		cell.stockQty.text = [NSString stringWithFormat: @"от: %@", [HedronCore dateFormat: order.odate]];
	}
	return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return [HedronCore getReportOrdersHeader: section];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    int section = indexPath.section;
	int row = indexPath.row;

	//open OrderStockEdit
	
	Order* order = (Order*)[(NSArray*)[[HedronCore getReportOrders] objectAtIndex: section] objectAtIndex: row];
	[HedronCore setCurrentEditableOrder:order];
	
	OrderEdit *orderEdit = [[OrderEdit alloc] initWithNibName:@"OrderEdit" bundle:nil];				
	orderEdit.editedOrder = order;
	[self.navigationController pushViewController:orderEdit animated:YES];
	[orderEdit release];
}

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
		int section = indexPath.section;
		int row = indexPath.row;
		orderToDelete = (Order*)[(NSArray*)[[HedronCore getReportOrders] objectAtIndex: section] objectAtIndex: row];
		
		if(orderToDelete){
			UIAlertView *alert = [[UIAlertView alloc] init];
			[alert setTitle:@"Изтриване!"];
			[alert setMessage:@"Желаете ли да изтриете избраната поръчка?"];
			[alert setDelegate:self];
			[alert addButtonWithTitle:@"Да"];
			[alert addButtonWithTitle:@"Не"];
			[alert show];
			[alert release];  
		}
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if(buttonIndex == 0){
		if([HedronCore deleteOrder: orderToDelete]){
			[self.tableView reloadData];
		}
	}
	orderToDelete = nil;
}


#pragma mark -


@end
